home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-07-21 | 4.2 KB | 145 lines | [TEXT/CWIE] |
- // ===========================================================================
- // CMessPopup.cp
- //
- // ©1997 Maxym Runov. All rights reserved.
- //
- // You are free to use this code in any project,
- // but the copyright remains with me.
- // Any questions and notes are welcome.
- //
- // E-mail: max@sunbay.crimea.ua
- // ===========================================================================
- // Version 1.0
-
- #include <UTextTraits.h>
- #include <UDrawingState.h>
- #include "CMessPopup.h"
-
- // Internal definition
- #define kMarkSimbol '•'
-
-
- // ---------------------------------------------------------------------------
- // Constructor
- // ---------------------------------------------------------------------------
-
- CMessPopup::CMessPopup(LStream *inStream) : LStdPopupMenu(inStream) {
- MenuHandle theMenuH;
-
- mPopupMenuResID = 0;
- mCommandNums = 0;
- mNumCommands = 0;
- theMenuH = GetMacMenuH();
- if(theMenuH) mPopupMenuResID = (*theMenuH)->menuID;
- ReadCommandNumbers();
- }
-
-
- // ---------------------------------------------------------------------------
- // Destructor
- // ---------------------------------------------------------------------------
-
- CMessPopup::~CMessPopup() {
- // Free command number table
- if(mCommandNums) ::DisposeHandle((Handle)mCommandNums);
- }
-
-
- #pragma mark -
-
-
- //-------------------------------------------------------------------------------------
- // SetValue
- //-------------------------------------------------------------------------------------
-
- void CMessPopup::SetValue(Int32 inValue) {
- if(GetValue() != inValue) {
- SetupCurrentMenuItem(inValue);
- if(inValue < mMinValue) inValue = mMinValue;
- else{
- if(inValue > mMaxValue) inValue = mMaxValue;
- }
- mValue = inValue;
- BroadcastValueMessage();
- Draw(nil);
- }
- }
-
-
- //-------------------------------------------------------------------------------------
- // SetupCurrentMenuItem
- //-------------------------------------------------------------------------------------
-
- void CMessPopup::SetupCurrentMenuItem(Int16 inCurrentItem) {
- MenuHandle theMenuH = GetMacMenuH();
- if(theMenuH) {
- if(GetValue() != inCurrentItem) {
- Int16 oldItem = GetValue();
- // Remove the current mark
- ::SetItemMark(theMenuH, oldItem, 0);
- }
- // Always make sure item is marked
- ::SetItemMark(theMenuH, inCurrentItem, kMarkSimbol);
- }
- }
-
-
- #pragma mark -
-
-
- // ---------------------------------------------------------------------------
- // ReadCommandNumbers
- // ---------------------------------------------------------------------------
-
- void CMessPopup::BroadcastValueMessage() {
- Int32 theValue = mValue;
- BroadcastMessage(CommandFromIndex(theValue), (void *)&theValue);
- }
-
-
- // ---------------------------------------------------------------------------
- // ReadCommandNumbers
- // ---------------------------------------------------------------------------
- // Get command numbers from the 'Mcmd' resource
- //
- // A 'Mcmd" resource with the same ID as the 'MENU' resource contains a
- // list of command numbers. The format of a 'Mcmd' resource is:
- // Number of Commands: n (2 bytes)
- // Command Num for Item 1 (4 bytes)
- // ...
- // Command Num for Item n (4 bytes)
-
- void CMessPopup::ReadCommandNumbers(void) {
- Int16 **theMcmdH;
-
- theMcmdH = (Int16 **)::GetResource('Mcmd', GetPopupMenuResID());
- if(theMcmdH != nil){
- mNumCommands = (*theMcmdH)[0];
- if(mNumCommands > 0){
- // Our command numbers list is the same as the 'Mcmd'
- // resource without the 2-byte count at the top. So we
- // can reuse the 'Mcmd' resource Handle by detaching it,
- // shifting the command numbers down by 2-bytes, and
- // resizing it.
-
- ::DetachResource((Handle)theMcmdH);
- mCommandNums = (Int32**)theMcmdH;
- Int32 commandsSize = mNumCommands * sizeof(Int32);
- ::BlockMoveData(*theMcmdH+1, *mCommandNums, commandsSize);
- ::SetHandleSize((Handle) mCommandNums, commandsSize);
- }
- }
- }
-
-
- // ---------------------------------------------------------------------------
- // CommandFromIndex
- // ---------------------------------------------------------------------------
- // Return the command number for a particular Menu item
-
- CommandT CMessPopup::CommandFromIndex(Int16 inIndex) const {
- Int32 theCommand = 0;
- if(inIndex <= mNumCommands) theCommand = (*mCommandNums)[inIndex-1];
- return theCommand;
- }
-